﻿using System;
using System.Text;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
using Stock;
using System.Data;
using WPFExcelReport;
using WPFExcelReport.Helpers;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
using Oracle.DataAccess;  
namespace WPFExcelReport
{
    class StockQueueLoader
    {

        static void Main(string[] args)
        {
            SETINTEXDB INTEXDB = new SETINTEXDB();
            string constr = "user id=intex;password=intexdbadmin;data source=intex1";
            OracleConnection OracleDB = new OracleConnection(constr);
            OracleDB.Open();
            OracleDataAdapter Adapter;
            DataSet SResult = new DataSet();
            OracleCommand Command = OracleDB.CreateCommand();
            try
            {
                string file = args[0];
                TextFileReader reader1 = new TextFileReader(file, ",");
                System.Text.Encoding encUTF8 = System.Text.Encoding.UTF8;
                string KRS = "", LFD = "", POS = "", DLNOTE = "";
                string WFID = "0";
                string Receiver = "";
                //System.Text.Encoding encGB2312= System.Text.Encoding.GetEncoding("GB2312");
                DataRow dwWF = INTEXDB.ICC_WFEVENTS.NewRow();
                dwWF["MANDANT"] = "00";
                dwWF["CONTROL"] = "NO ";
                dwWF["WFEVENTS"] = "MYGRVKPOS007";
                dwWF["WHAT"] = Helper.ConvertGB2312To8859P1("自动打印");
                dwWF["HOW"] = dwWF["WHAT"];
                int idxs = file.LastIndexOf('\\');
                int idxdot = file.LastIndexOf('.');
                string WFEUSER = "ITXV    ";
                if (idxs > 0 && idxdot > 0)
                {
                    WFEUSER = file.Substring(idxs+1, idxdot- idxs-1);
                }
                dwWF["WFEUSER"] = Helper.Left(WFEUSER.PadRight(8), 8);
                dwWF["ORDTYPE"] = " "; 
                dwWF["WFWHEN"] = DateTime.Now.ToString();
                Command = OracleDB.CreateCommand();
                Command.CommandText = "SELECT  SEQ_ICC_ITX.NEXTVAL AS SEQ FROM DUAL";
                Adapter = new OracleDataAdapter(Command);
                Adapter.ContinueUpdateOnError = true;
                Adapter.Fill(SResult);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    WFID = row["SEQ"].ToString();
                    dwWF["ROWNID"] = WFID;
                }
                foreach (var str in reader1)
                {
                    string text = str[0];
                    text = text.Replace("&a19C", "");

                    if (text.StartsWith("ORDTYPE*"))
                    {
                        string temp = text.Replace("ORDTYPE*", "").Replace("*", "");
                        dwWF["ORDTYPE"] = temp;
                    }
                    dwWF["KRS"] = "  ";
                    if (text.StartsWith("KRS*"))
                    {
                        string temp = text.Replace("KRS*", "").Replace("*", "");
                        dwWF["KRS"] = temp;
                    }
                    dwWF["LFD"] = "      ";
                    if (text.StartsWith("LFD*"))
                    {
                        string temp = text.Replace("LFD*", "").Replace("*", "");
                        dwWF["LFD"] = temp;
                    }
                    dwWF["POS"] = "   ";
                    if (text.StartsWith("POS*"))
                    {
                        string temp = text.Replace("POS*", "").Replace("*", "");
                        dwWF["POS"] = temp;
                    }
                    dwWF["P1"] = "   ";
                    if (text.StartsWith("DLNOTE*"))
                    {
                        string temp = text.Replace("DLNOTE*", "").Replace("*", "");
                        dwWF["P1"] = temp;
                    } 
                    dwWF["WFEHANDLER"] = "   ";
                    if (text.StartsWith("PROG*"))
                    {
                        string temp = text.Replace("PROG*", "").Replace("*", "").Trim();
                        Receiver = temp;
                        dwWF["WFEHANDLER"] = Receiver;
                    }
                    dwWF["WFSOURCE"] = "   ";
                    if (text.StartsWith("DOCTYPE*"))
                    {
                        string temp = text.Replace("DOCTYPE*", "").Replace("*", "");
                        dwWF["WFSOURCE"] = temp;
                    }
                }
                Command = OracleDB.CreateCommand();
                Command.CommandText = " INSERT INTO ICC_WFEVENTS(MANDANT, ROWNID, CONTROL,  WFEUSER, WFEVENTS, WFEHANDLER, WFSOURCE, WFWHEN, WHAT, HOW, ORDTYPE, KRS, LFD, POS, ARTTYPE, ARTICLE, ARTDES, VAR, COLOR, CDES, ASIZE, FINISH, FINDES, P1, P2, P3, P4, P5, P6, NEWUSER, NEWTIME)"
+ " VALUES (:MANDANT, :ROWNID, :CONTROL, :WFEUSER, :WFEVENTS, :WFEHANDLER, :WFSOURCE,:WFWHEN, :WHAT,:HOW, :ORDTYPE, :KRS, :LFD, :POS, ' ', '                    ', '                                        ', '   ', '          ', '                                        ', '       ', '          ', '                                        ',:P1, '                                        ', '                                        ', '                                        ', '                                        ', '                                        ',  :NEWUSER, :NEWTIME)";

                Command.Parameters.Add("MANDANT", dwWF["MANDANT"].ToString());
                Command.Parameters.Add("ROWNID", dwWF["ROWNID"].ToString());
                Command.Parameters.Add("CONTROL", dwWF["CONTROL"].ToString());
                Command.Parameters.Add("WFEUSER", dwWF["WFEUSER"].ToString());
                Command.Parameters.Add("WFEVENTS", dwWF["WFEVENTS"].ToString());
                Command.Parameters.Add("WFEHANDLER", dwWF["WFEHANDLER"].ToString());
                Command.Parameters.Add("WFSOURCE", dwWF["WFSOURCE"].ToString());
                Command.Parameters.Add("WFWHEN", dwWF["WFWHEN"].ToString());

                Command.Parameters.Add("WHAT", dwWF["WHAT"].ToString());
                Command.Parameters.Add("HOW", dwWF["HOW"].ToString());
                Command.Parameters.Add("ORDTYPE", dwWF["ORDTYPE"].ToString());
                Command.Parameters.Add("KRS", dwWF["KRS"].ToString());
                Command.Parameters.Add("LFD", dwWF["LFD"].ToString());
                Command.Parameters.Add("POS", dwWF["POS"].ToString());
                Command.Parameters.Add("P1", dwWF["P1"].ToString());
                
                Command.Parameters.Add("NEWUSER", dwWF["WFEUSER"].ToString());
                Command.Parameters.Add("NEWTIME", dwWF["WFWHEN"].ToString());
                Adapter = new OracleDataAdapter(Command);
                Adapter.ContinueUpdateOnError = true;
                Adapter.Fill(SResult);
                // Prepare queue
                if (OracleDB.State == ConnectionState.Closed)
                {
                    OracleDB.Open();
                }
                OracleAQQueue queue = new OracleAQQueue("INTEX.STOCKQQ", OracleDB);
                queue.MessageType = OracleAQMessageType.Udt;
                queue.EnqueueOptions.Visibility = OracleAQVisibilityMode.Immediate;
                OracleAQMessage enqMsg = new OracleAQMessage();
                enqMsg.Expiration = 1 * 60 * 60;
                enqMsg.SenderId = new OracleAQAgent("QUEUELOADER");
                enqMsg.Payload = new STOCK(Receiver, Convert.ToDecimal(WFID));
                queue.Enqueue(enqMsg);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

